﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;
using System.IO;

namespace HY.WEB.OA
{
    public partial class _tel_list : System.Web.UI.Page //PageBase
    {
        public string whe = "", stype = "";
        public string us, um,bh, tab = "comp_tel";
        protected void Page_Load(object sender, EventArgs e)
        {  
            us=Session["ID"].ToString();
            um = Session["user"].ToString();
            bh = Session["bh"].ToString();
           // ddlcomp.Visible = false;
            txtcomp.Visible = false;
            if (!IsPostBack)
            {           
               
                fill();
                string comp = ddlcomp.SelectedText;
                string person = ddlperson.SelectedText;
                string dts1 = date1.Text;
                string dts2 = date2.Text;
                string dtype = ddltype.SelectedValue;

                // comp = txtcomp.Text;
                if (comp != "")
                    whe = whe + " and cname like '%" + comp + "%' ";

                if (person != "")
                    whe = whe + " and pname='" + person + "'";


                if (dtype != "")
                    whe = whe + " and left(cbh,2)='" + dtype + "'";


                if (dts1 != "" & dts2 != "")
                    whe = whe + " and convert(char(10),pdtime,120)>=convert(char(10),convert(datetime,'" + dts1 + "'),120) and" +
            " convert(char(10),pdtime,120)<=convert(char(10),convert(datetime,'" + dts2 + "'),120)";
                BindGrid();
            }
        }

        private void fill()
        {
            DataTable du = BLL.Bllbase.get_table("company");
            ddlcomp.Items.Clear();
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlcomp.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["cname"].ToString(), du.Rows[i]["cbh"].ToString()));
            }
            ddlcomp.DataBind();
            ddlcomp.Items.Add(new ExtAspNet.ListItem("", ""));

                   
             du =BLL.Bllbase.get_table("sys_user");
            ddlperson.Items.Clear();
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlperson.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["user_name"].ToString(), du.Rows[i]["sbh"].ToString()));
            }
            ddlperson.DataBind();
            ddlperson.Items.Add(new ExtAspNet.ListItem("",""));

            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByValue(bh));
            ddlcomp.SelectedIndex = ddlcomp.Items.IndexOf(ddlcomp.Items.FindByValue(""));
        }

        //最姓名
        public  string getnm(string nmid)
        {
            BLL.HR.sys_user u = new BLL.HR.sys_user();
            return BLL.BLLfun.getname(nmid);
         }

        private void BindGrid()
        {
           
            DataTable  table2;

            table2 =BLL.Bllbase.get_table(tab,whe);
            DataView view1 = table2.DefaultView;
            view1.Sort = String.Format("{0} {1}","sid","desc");

            Grid2.DataSource = view1;
            Grid2.DataBind();

        }

      

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }

        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table; 
            table = BLL.Bllbase.get_table(tab, whe);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }




        protected void btnExport_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            //  Response.Clear();
            Response.Buffer = true;
            Response.Charset = "gb2312";

            Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
            Response.AppendHeader("content-disposition", "attachment;filename=\"" +
                HttpUtility.UrlEncode("员工考勤[" + DateTime.Now.ToString("yyyy-MM-dd") + "]", System.Text.Encoding.UTF8) + ".xls\"");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            Response.ContentType = "application/excel";
            Response.Write(GetGridTableHtml(Grid2));
            Response.End(); 
     
     
        }

    

        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "sub") //修改 
            {
                string dd = Grid2.DataKeys[e.RowIndex][1].ToString();
              
                DataTable dt1 =BLL.oa.leave_sub.getlist(dd);

                //Grid1.DataSource=dt1;
               // Grid1.DataBind();

               

                // txttitle.Text = dt1.Rows[0]["retitle"].ToString().Trim();
                // txtord.Text = dt1.Rows[0]["renm"].ToString().Trim();


                //ddluser.SelectedIndex = 
                //  ddluser.Items.IndexOf(ddluser.Items.FindByValue(getnm( dt1.Rows[0]["renm"].ToString().Trim())));
            }
            else if (e.CommandName == "Del")//删除 
            {

            } 

        }

       


        protected void btsearch_Click(object sender, EventArgs e)
        {
            string comp = ddlcomp.SelectedText.Trim();
            string person = ddlperson.SelectedText.Trim();
            string dts1 = date1.Text.Trim();
            string dts2 = date2.Text.Trim();
            string dtype = ddltype.SelectedValue.Trim();

            if (comp != "")
            whe = whe + " and cname like '%" + comp + "%' ";

            if (person != "")
                whe = whe + " and pname='"+person+"'";

            if (dts1 != ""&dts2!="")
            whe = whe + " and convert(char(10),pdtime,120)>=convert(char(10),convert(datetime,'" + dts1 + "'),120) and" +
    " convert(char(10),pdtime,120)<=convert(char(10),convert(datetime,'" + dts2 + "'),120)";
           
            if (dtype != "")
                whe = whe + " and left(cbh,2)='" + dtype + "'";
           // DataTable dt = BLL.Bllbase.get_table(tab,whe);
            //Grid2.PageIndex = 1;
            BindGrid();
            
       
        }

       
        
        private string GetGridTableHtml(Grid grid) 
        { 
            StringBuilder sb = new StringBuilder();
            sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
            sb.Append("<tr>"); 
            foreach (GridColumn column in grid.Columns) 
            { 
                sb.AppendFormat("<td>{0}</td>", column.HeaderText); 
            } 
            sb.Append("</tr>"); 
            foreach (GridRow row in grid.Rows)
            { 
                sb.Append("<tr>"); 
                foreach (object value in row.Values)
                { 
                    string html = value.ToString();
                    sb.AppendFormat("<td>{0}</td>", html); 
                }
                sb.Append("</tr>");
            } 
            sb.Append("</table>");
            return sb.ToString(); 
        } 


    
    }
}
